package com.jeasonzhao.report.model;

import com.jeasonzhao.commons.utils.Algorithms;

public class ReportParamCollection extends java.util.Vector<ReportParam>
{
    private static final long serialVersionUID = 1L;
    public static ReportParamCollection from(Iterable<ReportParam> colls)
    {
        if(null == colls)
        {
            return null;
        }
        ReportParamCollection coll = new ReportParamCollection();
        for(java.util.Iterator<ReportParam> it = colls.iterator();it.hasNext();)
        {
            coll.add(it.next());
        }
        return coll;
    }

    public String toString()
    {
        return toString("",false);
    }

    public void removeDupliactedItems()
    {
        removeDupliactedItems(false);
    }

    public void removeDupliactedItems(boolean isCaseSenstive)
    {
        if(this.size() < 2)
        {
            return;
        }
        ReportParamCollection newColl = new ReportParamCollection();
        for(ReportParam rp : this)
        {
            if(null == rp || Algorithms.isEmpty(rp.getParam())
               || null != newColl.findParameterByName(rp.getParam(),isCaseSenstive))
            {
                continue;
            }
            newColl.add(rp);
        }
        this.clear();
        for(ReportParam rp : newColl)
        {
            this.add(rp);
        }
    }

    public String toString(String str,boolean appendAtFirst)
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;n < this.size();n++)
        {
            if(null != str && (n > 0 || appendAtFirst))
            {
                strRet.append(str);
            }
            strRet.append(this.elementAt(n));
        }
        return strRet.toString();
    }

    public void removeByParameterName(String strParameterName)
    {
        removeByParameterName(strParameterName,false);
    }

    public void removeByParameterName(String strParameterName,boolean isCaseSensitive)
    {
        if(Algorithms.isEmpty(strParameterName))
        {
            return;
        }
        for(int n = 0;n < this.size();n++)
        {
            ReportParam p = this.get(n);
            if(null == p)
            {
                continue;
            }
            boolean b = isCaseSensitive ? p.getParam().equals(strParameterName) : p.getParam().equalsIgnoreCase(strParameterName);
            if(b)
            {
                this.remove(n);
                n--;
            }
        }
    }

    public ReportParam findParameterByName(String strParameterName)
    {
        return findParameterByName(strParameterName,false);
    }

    public ReportParam findParameterByName(String strParameterName,boolean isCaseSensitive)
    {
        for(ReportParam p : this)
        {
            if(null == p)
            {
                continue;
            }
            if(isCaseSensitive)
            {
                if(p.getParam().equals(strParameterName))
                {
                    return p;
                }
            }
            else
            {
                if(p.getParam().equalsIgnoreCase(strParameterName))
                {
                    return p;
                }
            }
        }
        return null;
    }
}
